import type { Transaction } from '../types'

const KEY = 'transactions'

function loadTransactionsFromLocalStorage(): Transaction[] {
  console.log('加载数据')
  const data = localStorage.getItem(KEY)
  const result: Transaction[] = []
  if (data) {
    result.push(...(JSON.parse(data) as Transaction[]))
  }
  return result
}

// 方法阐述默认值
const saveTransactionsToLocalStorage = (data: Transaction[] = []) => {
  console.log('存储数据成功')
  localStorage.setItem(KEY, JSON.stringify(data))
}

export { loadTransactionsFromLocalStorage, saveTransactionsToLocalStorage }
